<template>
  <d2-container>
    <el-alert
      title="由于功能特殊，请注意在需要时刷新您的浏览器（以重置路由设置）查看效果"
      type="warning"
      show-icon/>
    <el-form :inline="true" :model="dataForm">
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      row-key="menuId"
      border
      @selection-change="selectionChangeHandle"
      style="width: 100%; ">
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        min-width="150"
        label="名称" >
      </el-table-column>
      <el-table-column
        prop="parentName"
        header-align="center"
        align="center"
        width="120"
        label="上级菜单">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        label="图标">
        <template slot-scope="scope">
          <i :class="'fa fa-' + (scope.row.icon ? scope.row.icon: 'folder-o')"></i>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type === 0" size="small">目录</el-tag>
          <el-tag v-else-if="scope.row.type === 1" size="small" type="success">菜单</el-tag>
          <el-tag v-else-if="scope.row.type === 2" size="small" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderNum"
        header-align="center"
        align="center"
        label="排序号">
      </el-table-column>
      <el-table-column
        prop="isLock"
        header-align="center"
        align="center"
        label="是否锁定">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isLock" size="small" type="danger">锁定</el-tag>
          <el-tag v-else size="small" type="success">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="path"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="菜单URL">
      </el-table-column>
      <el-table-column
        prop="perms"
        header-align="center"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
        label="授权标识">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.menuId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.menuId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="addOrUpdateFinish"></add-or-update>
  </d2-container>
</template>

<script>
import util from '@/libs/util.js'
import AddOrUpdate from './menu-add-or-update'
import { reqList, reqDelete } from '@api/userperm.api.fetch'
export default {
  name: 'system-menu',
  data () {
    return {
      dataForm: {},
      dataList: [],
      dataListSelections: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      uuid: ''
    }
  },
  components: {
    AddOrUpdate
  },
  mounted () {
    this.uuid = util.cookies.get('uuid')
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.addOrUpdateVisible = false
      this.dataListLoading = true
      reqList().then(res => {
        this.dataList = res.data
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    addOrUpdateFinish () {
      // location.reload()
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.menuId
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        reqDelete(ids).then(res => {
          this.$message({
            message: '操作成功，若要看到菜单栏的改变请按F5刷新页面',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList()
            }
          })
        })
      }).catch(() => {})
    }
  }
}
</script>
